package com.example.demo.handler;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * @author long
 * @version 1.0.0
 * @ClassName MessageReceiver
 * @Description TODO
 * @createTime 2022.03.01 01:26
 */
@Component
public class MessageReceiver {

    Logger logger = LoggerFactory.getLogger(MessageReceiver.class);

    /**
     * 监听转发队列  走逻辑判断，尚未支付且超过过期时间的订单号设置为失效订单
     *
     * @param message 信息包装类
     * @param channel 通道
     */
    @RabbitListener(queues = {"ORDER_REDIRECT_QUEUE"})
    public void redirect(Message message, Channel channel) throws IOException {
        // 从队列中取出订单号
        byte[] body = message.getBody();
        String orderNo = new String(body, "UTF-8");
        logger.info("消费消息，订单号为[{}]", orderNo);
        // 确认消息有没有被收到,false表示手动确认  在处理完消息时，返回应答状态
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
